package com.rmondjone.face.ViewModel

import com.ant.http.Bean.HttpResult
import retrofit2.Call
import retrofit2.Response

/**
 * copyright：
 * @author：anthui creation date： .
 * @since: 1.0.0
 * @describe:
 */
abstract class OnCallBack<T> : retrofit2.Callback<HttpResult<T>> {

    override fun onResponse(call: Call<HttpResult<T>>, response: Response<HttpResult<T>>) {
        response.body()?.let {
            if (it.code == 1 && it.data != null) {

                onSuccess(it.data!!)

                return
            } else {

                onFailure(it.code, it.message ?: "网络不稳当")

                return

            }

        }
//        Toast.makeText(MyApplication.application, "网络不稳当", Toast.LENGTH_SHORT).show()
        onFailure(-1, "网络不稳当")
    }

    override fun onFailure(call: Call<HttpResult<T>>, t: Throwable) {
//        onFailure(-1, "错误", t)
//        Toast.makeText(MyApplication.application, "识别失败", Toast.LENGTH_SHORT).show()

        onFailure(-1, "网络不稳当")
    }

    abstract fun onSuccess(item: T)

    abstract fun onFailure(code: Int, msg: String? = null, er: Throwable? = null)
}